package com.lhys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhys.mapper.AssetDeviceUseMapper;
import com.lhys.pojo.AssetDeviceUse;
import com.lhys.service.AssetDeviceUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 设备用途 服务实现类
 * </p>
 *
 * @author 小七
 * @since 2021-05-06
 */
@Service
public class AssetDeviceUseServiceImpl implements AssetDeviceUseService {
    @Autowired
    private AssetDeviceUseMapper deviceUseMapper;

    @Override
    @Transactional
    public Boolean saveAssetDeviceUse(AssetDeviceUse assetDeviceUse) {
        try {
            if (assetDeviceUse.getId() == null) {
                if (assetDeviceUse.getCreateTime() == null) {
                    assetDeviceUse.setCreateTime(new Date());
                }
                return deviceUseMapper.addAssetDeviceUse(assetDeviceUse) > 0;
            }
            return deviceUseMapper.updateAssetDeviceUse(assetDeviceUse) > 0;
        } catch (Exception e) {
            System.out.println("重复添加");
            return null;
        }
    }

    @Override
    @Transactional
    public Boolean delAssetDeviceUseById(Integer id) {
        return deviceUseMapper.delAssetDeviceUse(id) > 0;
    }

    @Override
    public AssetDeviceUse getAssetDeviceUseById(Integer id) {
        return deviceUseMapper.getAssetDeviceUseById(id);
    }

    @Override
    public PageInfo<AssetDeviceUse> getAssetDeviceUseList(AssetDeviceUse assetDeviceUse, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AssetDeviceUse> list = deviceUseMapper.getAssetDeviceUseByCodeAndTitleOrRemarkList(assetDeviceUse);
        PageInfo<AssetDeviceUse> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

}
